package com.cn.yds.bdy.controller;

import com.cn.yds.bdy.dao.model.UserModel;
import com.cn.yds.bdy.service.UserService;
import com.cn.yds.bdy.vo.ResponseResult;
import com.cn.yds.bdy.vo.UserProfileVo;
import com.cn.yds.bdy.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("user")
@Slf4j
@Api(value = "User operation Api")
public class UserController {


    @Autowired
    private UserService userService;


    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ApiOperation(value = "user login validation")
    public ResponseResult login(@RequestBody @Valid UserVo userVo) {
        UserModel result = userService.getUserByLoginIdAndPw(userVo);
        Map<String, Object> map = new HashMap<>();
        Integer s = result == null ? -1 : 1;

        map.put("status", s);
        map.put("info", result);
        return ResponseResult.ok(map);
    }


    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "user update")
    public ResponseResult update(@RequestBody UserProfileVo userProfileVo) {
        // TODO: 2019/1/8  creat vo to recive data from customer , then  convert to UserModel (Query by login_id), save it
        userService.updateUserInfo(userProfileVo);
        return ResponseResult.ok(1);
    }


    @RequestMapping(value = "profile", method = RequestMethod.POST)
    @ApiOperation(value = "user profile")
    public ResponseResult profile(@RequestBody UserVo userVo) {
        Assert.isTrue(!StringUtils.isEmpty(userVo.getAccount()), "Account is must be not empty!");

        UserModel userByLoginId = userService.getUserByLoginId(userVo.getAccount());
        UserProfileVo userProfileVo = new UserProfileVo();
        BeanUtils.copyProperties(userByLoginId, userProfileVo);
        return ResponseResult.ok(userProfileVo);
    }

}
